package com.simpletour.qa.service.controller;

import com.simpletour.qa.common.http.ObjectRestResponse;
import com.simpletour.qa.common.http.ResponseCode;
import com.simpletour.qa.common.http.ResponseMsg;
import com.simpletour.qa.service.entity.Question;
import com.simpletour.qa.service.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


@RequestMapping(value = "/question")
@RestController
public class QuestionController {

    @Autowired
    private QuestionService questionService;

    @RequestMapping(value = "", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public ObjectRestResponse add(@RequestBody @Valid Question question, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            List<FieldError> errorList = bindingResult.getFieldErrors();
            return ObjectRestResponse.builder().code(ResponseCode.PARAM_CHECK_ERROR).msg(errorList.toString()).build();
        }
        questionService.save(question);
        return ObjectRestResponse.builder().code(ResponseCode.SUCCESS).msg(ResponseMsg.SUCCESS).build();
    }


    @RequestMapping(value = "/{id}", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
    public ObjectRestResponse get(@PathVariable Integer id) {
        Question question = questionService.findById(id).orElse(null);
        return ObjectRestResponse.builder().code(ResponseCode.SUCCESS).result(question).msg(ResponseMsg.SUCCESS).build();
    }

   /*
    @RequestMapping(value = "/{id}",method = RequestMethod.PUT)
    public ObjectRestResponse update(@PathVariable Integer id, @RequestBody Question entity){
        try {
            ReflectionUtils.setPrimaryKey(entity,id);
        } catch (Exception e) {
            return ObjectRestResponse.builder().code(ResponseCode.SERVER_INTERNAL_ERROR);
        }
        baseBiz.save(entity);
        return ObjectRestResponse.builder().code(ResponseCode.SUCCESS).msg(ResponseMsg.SUCCESS);
    }*/


    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE, produces = {"application/json;charset=UTF-8"})
    public ObjectRestResponse remove(@PathVariable Integer id) {
        questionService.deleteById(id);
        return ObjectRestResponse.builder().code(ResponseCode.SUCCESS).msg(ResponseMsg.SUCCESS).build();
    }


    @RequestMapping(value = "/all", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
    public ObjectRestResponse all(@RequestParam(value = "page", defaultValue = "0") Integer page,
                                  @RequestParam(value = "fetch", defaultValue = "15") Integer fetch) {
        List<Question> all = questionService.findAll(page, fetch);
        return ObjectRestResponse.builder().code(ResponseCode.SUCCESS).result(all).msg(ResponseMsg.SUCCESS).build();
    }

    @RequestMapping(value = "/search",method = RequestMethod.GET,produces = {"application/json;charset=UTF-8"})
    public ObjectRestResponse search(@RequestParam(value = "page",defaultValue = "0") Integer page,
                                     @RequestParam(value = "fetch",defaultValue = "15") Integer fetch,
                                     @RequestParam(value = "keyWord") String keyWord,
                                     @RequestParam(defaultValue = "title",value = "searchType") String searchType){
        List<Question> questions = questionService.search(page,fetch,keyWord,searchType);
        return ObjectRestResponse.builder().code(ResponseCode.SUCCESS).result(questions).msg(ResponseMsg.SUCCESS).build();
    }
}


